package com.reliable.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.dto.user.ReliableUserLite;
import com.reliable.api.param.PageParam;
import com.reliable.api.param.user.UserUpdateParam;
import com.reliable.api.param.user.UserUpdatePasswordParam;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.framework.utils.JwtUtils;
import com.reliable.api.param.user.LoginUserPasswordParam;
import com.reliable.api.param.user.UserSaveParam;
import com.reliable.service.domain.ReliableUser;
import com.reliable.service.service.IReliableUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户列Controller
 * 
 * @author ruoyi
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/reliable/user")
@Api(value = "用户列控制器", tags = {"用户列管理"})
public class ReliableUserController {

    @Autowired
    private IReliableUserService reliableUserService;


    /**
     * 用户注册
     */
    @ApiOperation("用户注册")
    @PostMapping("/userSava")
    public AjaxResult save (@RequestBody UserSaveParam param) {
        return reliableUserService.checkUser(param);
    }

    /**
     * 用户登录 账号and密码
     */
    @ApiOperation("用户登录 账号and密码")
    @PostMapping("/login/userPassword")
    public AjaxResult loginUserPassword(@RequestBody LoginUserPasswordParam param) {
        return reliableUserService.loginByUserPassword(param);
    }

    /**
     * 退出登录
     */
    @ApiOperation("退出登录")
    @PostMapping("login/logout")
    public AjaxResult logout() {
        ReliableUserLite user = JwtUtils.getCurrentUser();
        return AjaxResult.success(user);
    }

    /**
     * 完善用户信息
     */
    @ApiOperation("完善用户信息")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody UserUpdateParam param) {
        return reliableUserService.update(param);
    }

    /**
     * 修改用户密码
     */
    @ApiOperation("修改用户密码")
    @PostMapping("/update/password")
    public AjaxResult updatePassword(@RequestBody UserUpdatePasswordParam param) {
        return reliableUserService.updatePassword(param);
    }

    /**
     * 获取当前用户信息
     */
    @ApiOperation("获取当前用户信息")
    @PostMapping("/userInfo")
    public AjaxResult info() {
        ReliableUserLite user = JwtUtils.getCurrentUser();
        Long id = user.getId();
        ReliableUser reliableUser = reliableUserService.getById(id);
        return AjaxResult.success(reliableUser);
    }



   /**
    * 查询用户列列表 使用pageherp
    */
   @ApiOperation("查询用户列列表")
   @PostMapping("/userList")
   public AjaxResult list(@RequestBody PageParam param) {
       ReliableUserLite currentUser = JwtUtils.getCurrentUser();
       if (!currentUser.getUserType().equals(1L))
       {
           return AjaxResult.warn("无权限");
       }
       IPage<ReliableUser> page = reliableUserService.page(new Page<>(param.getPageNum(), param.getPageSize()));
       TableDataInfo data = new TableDataInfo(page.getRecords(), (int) page.getTotal());
       return AjaxResult.success(data);
   }

    private TableDataInfo getDataTable(List<ReliableUser> list) {
       return new TableDataInfo(list, list.size());
    }
//
//    /**
//     * 导出用户列列表
//     */
//    @ApiOperation("导出用户列列表")
//    @PreAuthorize("@ss.hasPermi('system:user:export')")
//    @Log(title = "用户列", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(reliableUser reliableUser) {
//        List<reliableUser> list = reliableUserService.list(new QueryWrapper<reliableUser>(reliableUser));
//        ExcelUtil<reliableUser> util = new ExcelUtil<reliableUser>(reliableUser.class);
//        return util.exportExcel(list, "用户列数据");
//    }
//
//    /**
//     * 获取用户列详细信息
//     */
//    @ApiOperation("获取用户列详细信息")
//    @PreAuthorize("@ss.hasPermi('system:user:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return AjaxResult.success(reliableUserService.getById(id));
//    }
//
//    /**
//     * 新增用户列
//     */
//    @ApiOperation("新增用户列")
//    @PreAuthorize("@ss.hasPermi('system:user:add')")
//    @Log(title = "用户列", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody reliableUser reliableUser) {
//        return toAjax(reliableUserService.save(reliableUser));
//    }
//
//    /**
//     * 修改用户列
//     */
//    @ApiOperation("修改用户列")
//    @PreAuthorize("@ss.hasPermi('system:user:edit')")
//    @Log(title = "用户列", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody reliableUser reliableUser) {
//        return toAjax(reliableUserService.updateById(reliableUser));
//    }
//
//    /**
//     * 删除用户列
//     */
//    @ApiOperation("删除用户列")
//    @PreAuthorize("@ss.hasPermi('system:user:remove')")
//    @Log(title = "用户列", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(reliableUserService.removeByIds(Arrays.asList(ids)));
//    }
}
